package com.yunheone.Jdbc;

import com.yunheone.control.Indexcontrol;
import com.yunheone.mode.User;
import com.yunheone.view.user.AddUser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/28 0028.
 */
public class UserJdbc {
    public static String loginJdbc(int type,String name){
        String ty = ty ( type );
        String sql="select * from user where type=? and useName=?";
        PreparedStatement item = JdbcHelp.item ( sql ,ty,name);
        ResultSet result=JdbcHelp.Query ( item );
        String password=null;
            try {
                while (result.next ()) {
                    password = result.getString ( "passWord" );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                JdbcHelp.close ( result );
            }
        return password;
    }

    /**
     * 查
     * @return
     */
    public static List<User> chat(){
        List<User> list=new ArrayList<> (  );
        String s="select * from user ";
        PreparedStatement item = JdbcHelp.item ( s);
        ResultSet query = JdbcHelp.Query ( item );
        try {
           while (query.next ()){
               String no = query.getString ( "no" );
               String useName = query.getString ( "useName" );
               String passWord = query.getString ( "passWord" );
               String type=query.getString ( "type" );
               String valName=query.getString ( "valName" );
               User user = new User ( no, useName, valName, passWord, type );
               list.add ( user );
           }
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return list;
    }



    /**
     * 判断登录用户类型
     * @param type
     * @return
     */
    public static String ty(int type){
        String Type=null;
        switch (type){
            case 1:Type="管理员";break;
            case 2:Type="讲师";break;
        }
        return Type;
    }
}
